# 登录对话框
from PyQt5.QtWidgets import QMessageBox, QDialog

from app.view import LoginPage


class LoginDialog(QDialog):
    """登录对话框"""

    def __init__(self):
        super().__init__()
        self.ui = LoginPage.Ui_QDialog()
        self.ui.setupUi(self)
        self.init_ui()

    def init_ui(self):
        """初始化界面组件"""
        self.ui.login_button.clicked.connect(self.check_credentials)
        self.ui.cancel_button.clicked.connect(self.reject)
        self.ui.username_input.setText("admin")
        self.ui.password_input.setText("123456")

    def check_credentials(self):
        """验证用户凭据"""
        username = self.ui.username_input.text().strip()
        password = self.ui.password_input.text().strip()

        if not username or not password:
            QMessageBox.warning(self, "输入错误", "用户名和密码不能为空")
            return

        if username == "admin" and password == "123456":
            self.accept()
        else:
            QMessageBox.critical(self, "登录失败", "用户名或密码错误")
            self.ui.password_input.clear()
            self.ui.password_input.setFocus()
